// components/drawer/drawer.js
Component({
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    userInfo: {
      type: Object,
      value: {}
    }
  },

  data: {
    animating: false
  },

  methods: {
    /**
     * 点击遮罩关闭抽屉
     */
    onMaskTap() {
      this.closeDrawer();
    },

    /**
     * 关闭抽屉
     */
    closeDrawer() {
      this.setData({ animating: true });
      this.triggerEvent('close');
      
      setTimeout(() => {
        this.setData({ animating: false });
      }, 300);
    },

    /**
     * 点击用户信息区
     */
    onUserInfoTap() {
      if (!this.data.userInfo.nickName) {
        // 未登录，触发登录
        this.triggerEvent('login');
      } else {
        // 已登录，跳转到个人中心
        this.navigateTo('profile');
      }
    },

    /**
     * 点击菜单项
     */
    onMenuTap(e) {
      const { page } = e.currentTarget.dataset;
      this.navigateTo(page);
    },

    /**
     * 退出登录
     */
    onLogout() {
      wx.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            this.triggerEvent('logout');
            this.closeDrawer();
          }
        }
      });
    },

    /**
     * 页面跳转
     */
    navigateTo(page) {
      // 检查页面是否存在
      const validPages = ['profile', 'history', 'settings'];
      
      if (validPages.includes(page)) {
        this.closeDrawer();
        wx.navigateTo({
          url: `/pages/${page}/${page}`
        });
      } else {
        // TODO页面
        wx.showToast({
          title: '功能开发中...',
          icon: 'none',
          duration: 1500
        });
      }
    }
  }
});
